/**
 * author:Administrator
 * date:2016/1/4
 * desc:TODO
 */

'use strict';

var Model = require('../model/order.js');
var counter = require('./counter.js');
var myCounter = counter.bindCounter(counter.TypesEnum.orderid);
var dao = {};

module.exports = dao;

dao.empty = function* () {
  return yield Model.remove({});
};

dao.addFreshOrder = function*(oid) {
  if (typeof oid === 'undefined') oid = yield myCounter();
  return yield new Model({
    oid: oid,
    state: 'unpay'
  }).save();
};

/**
 * 添加一个"空"的优惠券
 */
dao.pay = function*(oid) {
  return yield Model.findOneAndUpdate(
    {oid: oid, state: 'unpay'},
    {$set: {state: 'paying'}},
    {upsert: false, 'new': true});
};
